<template>
  <div class="login-box">
    <div class="box-left">
      <div class="box-title">
        <div class="box-title-1"><img src="~@/assets/title_1.png" alt=""></div>
        <div class="box-title-2"><img src="~@/assets/title_2.png" alt=""></div>
      </div>
      <div class="border-bg-1"></div>
      <div class="border-bg-2"></div>
      <div class="border-bg-3">
        <div class="div1"></div>
        <div class="div2"></div>
      </div>
      <div class="border-bg-4">
        <div class="div1"></div>
        <div class="div2"></div>
      </div>
    </div>
    <div class="box-right">
      <a-form-model class="user-layout-login" @keyup.enter.native="handleSubmit">
        <h2 class="login-title">登录</h2>
        <login-account ref="alogin" @validateFail="validateFail" @success="requestSuccess" @setCertShow="onSetCertShow"
          @fail="requestFailed"></login-account>
        <a-form-model-item class="autoLogin">
          <a-checkbox @change="handleRememberMeChange" default-checked>记住密码</a-checkbox>
          <!-- <router-link :to="{ name: 'alteration' }" class="forge-password" style="float: right;">
            忘记密码
          </router-link> -->
          <!-- <router-link :to="{ name: 'register'}" class="forge-password" style="float: right;margin-right: 10px" >
            注册账户
          </router-link> -->
        </a-form-model-item>

        <a-form-item style="margin-top:40px">
          <a-button size="large" type="primary" shape="round" htmlType="submit" class="login-button" :loading="loginBtn"
            @click.stop.prevent="handleSubmit" :disabled="loginBtn">登录
          </a-button>
        </a-form-item>
        <div v-if="isCertShow">
          <a-icon type="download" style="color:#1890FF;" />
          <a-button @click="onCertDownLoap()" type="link" style="padding: 0;margin-left:10px;">USBKEY控件下载</a-button>
        </div>
      </a-form-model>
    </div>
    <two-step-captcha v-if="requiredTwoStepCaptcha" :visible="stepCaptchaVisible" @success="stepCaptchaSuccess"
      @cancel="stepCaptchaCancel"></two-step-captcha>
    <login-select-tenant ref="loginSelect" @success="loginSelectOk"></login-select-tenant>
    <!-- <third-login ref="thirdLogin"></third-login> -->
  </div>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN, ENCRYPTED_STRING } from '@/store/mutation-types'
import ThirdLogin from './third/ThirdLogin'
import LoginSelectTenant from './LoginSelectTenant'
import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
import { getEncryptedString } from '@/utils/encryption/aesEncrypt'
import { timeFix } from '@/utils/util'

import LoginAccount from './LoginAccount'
import LoginPhone from './LoginPhone'

export default {
  components: {
    LoginSelectTenant,
    TwoStepCaptcha,
    ThirdLogin,
    LoginAccount,
    LoginPhone
  },
  data() {
    return {
      customActiveKey: 'tab1',
      rememberMe: true,
      loginBtn: false,
      requiredTwoStepCaptcha: false,
      stepCaptchaVisible: false,
      encryptedString: {
        key: '',
        iv: ''
      },
      isCertShow: true
    }
  },
  created() {
    Vue.ls.remove(ACCESS_TOKEN)
    this.getRouterData()
    this.rememberMe = true
  },
  methods: {
    handleTabClick(key) {
      this.customActiveKey = key
    },
    handleRememberMeChange(e) {
      this.rememberMe = e.target.checked
    },
    /**跳转到登录页面的参数-账号获取*/
    getRouterData() {
      this.$nextTick(() => {
        let temp = this.$route.params.username || this.$route.query.username || ''
        if (temp) {
          this.$refs.alogin.acceptUsername(temp)
        }
      })
    },

    //登录
    handleSubmit() {
      this.loginBtn = true
      if (this.customActiveKey === 'tab1') {
        // 使用账户密码登录
        this.$refs.alogin.handleLogin(this.rememberMe)
      } else {
        //手机号码登录
        this.$refs.plogin.handleLogin(this.rememberMe)
      }
    },
    // 校验失败
    validateFail() {
      this.loginBtn = false
    },
    // 登录后台成功
    requestSuccess(loginResult) {
      this.$refs.loginSelect.show(loginResult)
    },
    //登录后台失败
    requestFailed(err) {
      let description = ((err.response || {}).data || {}).message || err.message || '请求出现错误，请稍后再试'
      this.$refs.alogin.handleUserPassword(description)
      // this.$notification['error']({
      //   message: '登录失败',
      //   description: description,
      //   duration: 4
      // })
      //账户密码登录错误后更新验证码
      if (this.customActiveKey === 'tab1' && description.indexOf('密码错误') > 0) {
        this.$refs.alogin.handleChangeCheckCode()
        this.$refs.alogin.handleUserPassword(description)
      }
      this.loginBtn = false
    },
    loginSelectOk() {
      this.loginSuccess()
    },
    //登录成功
    loginSuccess() {
      this.$router.push({ path: '/dashboard/analysis' }).catch(() => {
        console.log('登录跳转首页出错,这个错误从哪里来的')
      })
      this.$notification.success({
        message: '欢迎',
        description: `${timeFix()}，欢迎回来`
      })
    },

    stepCaptchaSuccess() {
      this.loginSuccess()
    },
    stepCaptchaCancel() {
      this.Logout().then(() => {
        this.loginBtn = false
        this.stepCaptchaVisible = false
      })
    },
    //获取密码加密规则
    getEncrypte() {
      var encryptedString = Vue.ls.get(ENCRYPTED_STRING)
      if (encryptedString == null) {
        getEncryptedString().then(data => {
          this.encryptedString = data
        })
      } else {
        this.encryptedString = encryptedString
      }
    },
    onCertDownLoap() {
      let dom = document.createElement("a");
      dom.href = "/file/GM3000_mPlugin_Setup(202111打包证书客户端).exe";
      dom.download = "GM3000_mPlugin_Setup(202111打包证书客户端).exe";
      document.body.appendChild(dom);
      // 点击下载
      dom.click();
      document.body.removeChild(dom);
    },
    onSetCertShow(val) {
      this.isCertShow = val
    }
  }
}
</script>
<style lang="less" scoped>
@color1: #0943a4;
@color2: #ffffff;

.login-box {
  width: 100%;
  min-height: 100%;
  display: flex;
  flex-direction: row;
  align-items: stretch;

  .box-left {
    width: 100%;
    min-height: 100%;
    background-image: url("~@/assets/login-bg.jpg");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    position: relative;

    .box-title {
      padding: 80px 0 0 30px;

      .box-title-1 img {
        height: 16vh;
      }

      .box-title-2 img {
        height: 18vh;
      }
    }

    .border-bg-1 {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 44%;
      height: 58%;
      background-image: url("~@/assets/border-bg-1.png");
      background-repeat: no-repeat;
      background-size: contain;
    }

    .border-bg-2 {
      position: absolute;
      right: 0;
      bottom: 0;
      width: 26%;
      height: 38%;
      background-image: url("~@/assets/border-bg-2.png");
      background-repeat: no-repeat;
      background-size: cover;
    }

    .border-bg-3 {
      position: absolute;
      right: 0;
      top: 14%;
      margin-right: -60px;
      width: 120px;
      height: 120px;
      display: flex;

      div.div1 {
        width: 50%;
        height: 100%;
        background-color: @color2;
        border-radius: 120px 0 0 120px;
      }

      div.div2 {
        width: 50%;
        height: 100%;
        background-color: @color1;
        border-radius: 0 120px 120px 0;
      }
    }

    .border-bg-4 {
      position: absolute;
      right: 0;
      bottom: 14%;
      margin-right: -65px;
      width: 130px;
      height: 140px;
      display: flex;

      div.div1 {
        width: 50%;
        height: 8px;
        background: @color2;
        position: relative;

        &::before {
          position: absolute;
          top: 65px;
          content: '';
          width: 100%;
          height: 100%;
          background: inherit;
        }

        &::after {
          position: absolute;
          bottom: -130px;
          content: '';
          width: 100%;
          height: 100%;
          background: inherit;
        }
      }

      div.div2 {
        width: 50%;
        height: 8px;
        background: @color1;
        position: relative;

        &::before {
          position: absolute;
          top: 65px;
          content: '';
          width: 100%;
          height: 100%;
          background: inherit;
        }

        &::after {
          position: absolute;
          bottom: -130px;
          content: '';
          width: 100%;
          height: 100%;
          background: inherit;
        }
      }
    }
  }

  .box-right {
    width: 100%;
    min-height: 100%;
    flex-direction: column;
    display: flex;
    // align-items: center;
    justify-content: center;
    flex: 0 0 43%;
    width: 43%;

    .login-title {
      font-size: 36px;
      margin-bottom: 50px;
      color: @color1;
    }

    .user-layout-login {
      margin: 0 120px;

      label {
        font-size: 14px;
      }

      .getCaptcha {
        display: block;
        width: 100%;
        height: 40px;
      }

      .forge-password {
        font-size: 14px;
      }

      button.login-button {
        padding: 0 45px;
        font-size: 16px;
        height: 40px;
        background-color: @color1;
        border-color: @color1;
        // width: 100%;
      }

      .user-login-other {
        text-align: left;
        margin-top: 24px;
        line-height: 22px;

        .item-icon {
          font-size: 24px;
          color: rgba(0, 0, 0, 0.2);
          margin-left: 16px;
          vertical-align: middle;
          cursor: pointer;
          transition: color 0.3s;

          &:hover {
            color: #1890ff;
          }
        }

        .register {
          float: right;
        }
      }

    }

    /deep/.autoLogin {
      margin-bottom: 1vh !important;

      .ant-checkbox-checked .ant-checkbox-inner {
        background-color: @color1;
        border-color: @color1;
      }
    }
  }
}
</style>
<style>
.valid-error .ant-select-selection__placeholder {
  color: #f5222d;
}
</style>
